<?php

namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Models\Option;
use App\Models\Article;
use Carbon\Carbon,DB;
use Wechat;
class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        return redirect(url('/allactivity'));
        $wechat = app('wechat');
        $wechatUser = Wechat::user(); // 用户服务
        $wuser = session('wechat.oauth_user');
        //var_dump($wuser);
        //var_dump($wechatUser);
        $user = $request->user();
        //var_dump($user);
        $options = Option::where('status','!=',0)->get();
        $articles = Article::where('status','=',1)->where('ishome','=',1)->limit(3)->get();
        //经纪公告
        $Carbon = new Carbon();
        //dd($Carbon);
        $popup_article = Article::where('status','=',1)->where('popup','=',1)->where('created_at','>',Carbon::parse('-1 months')->toDateTimeString())->orderBy('sort','desc')->orderBy('id','desc')->first();
        if($popup_article){
            if($request->session()->has('popup_article')){
                $popup_article = null;
            }else{
                session(['popup_article' => $popup_article]);
            }
            
        }
        /*
        if($user){
            $state = DB::table('user_article_state')->where('user_id','=',$user->id)->where('article_id','=',$popup_article->id)->first();
            if($state){
                $popup_article = null;
            }else{
                DB::table('user_article_state')->insert(['user_id'=>$user->id,'article_id'=>$popup_article->id]);
            }

        }
        */
        //var_dump($articles);
        return view('home.index.index',compact('options','articles','popup_article'));
    }
}
